import {Component,OnInit} from '@angular/core';
import {Router, Params, ActivatedRoute} from '@angular/router';
import {UrlUtils} from '../../utils/url.util';
import {UserService} from '../../service/user/user.service';
import {DateUtils} from "../../utils/date.util";
import {InjuryBasePriceService} from "../../service/injuryBasePriceList/injuryBasePriceList.service";
import {InsuraceCompanyService} from "../common/insuraceCompanyList/insuraceCompany.service.ts";


declare var layer: any;
declare var $:any;
declare var is:any;
@Component({
  selector: 'injuryBasePriceListComponent',
  templateUrl: 'injuryBasePriceList.component.html'
})

export class InjuryBasePriceListComponent implements  OnInit{

  injuryBasePriceList: any;
  injurySiteTitle: any;
  feetype:number =32;
  diagnosisName:string;
  // currentPage:number;
  // pageSize:number;
  branchInsuranceCompanyId:number;
  saveFlag:boolean = false;
  private insuraceCompanyList: any [];
  pageModel:any;


  private  feeBasePriceModel = {
    feetype:0,
    branchInsuranceCompanyName:'',
    diagnosisName:'',
    diagnosisSubName:'',
    basePrice:0,
    id:0,
    diagnosisMainId:0,
    branchInsuranceCompanyId:0,
    version:0
  };
  OperationWinTitle:string;

  // private isFirstPage:boolean;
  // private isLastPage:boolean;
  // private pageCounts:number;
  // private pageNumber;number;
  feetypelist:any;

  constructor(private router: Router,
              private route: ActivatedRoute,
              private injuryBasePriceService: InjuryBasePriceService,
              private urlUtils: UrlUtils,
              private dateUtil: DateUtils,
              private userService: UserService,
              private companyService: InsuraceCompanyService,

               ) {}

  ngOnInit() {
    this.pageModel = {"currentPageNum":1};
    var url=this.urlUtils.getBackendApiUrl("injuryBasePrice/uploadData");
    $("#form1").attr("action",url);
    this.branchInsuranceCompanyId=0;
    this.feetype = 0;
    this.diagnosisName="";
    this.injuryBasePriceList = this.getInjuryBasePriceList();
    this.injurySiteTitle = [{name: '序号'},{name: '保险公司'},{name: '诊断信息'},{name: '费用类型'},{name: '基价'},{name: '操作'}];

    this.feetypelist = [{id:0,name: '---请选择---'},{id:1,name: '医疗费'},{id:2,name: '内固定器材'},{id:3,name: '伙补费'},{id:4,name: '牙费'},
      {id:5,name: '残疾赔偿金'},{id:6,name: '住宿费'}, {id:7,name: '交通费'}, {id:8,name: '误工费'},  {id:9,name: '续医费'},
      {id:10,name: '护理费'},{id:11,name: '抚养费'},{id:12,name: '营养费'},{id:13,name: '死亡赔偿金'},{id:14,name: '丧葬费'},{id:15,name: '精神抚慰金'},
      {id:16,name: '事故处理人员费用'},{id:17,name: '住院费'},{id:18,name: '残疾器具费'},{id:19,name: '定损合计金额'},{id:20,name: '医疗费发票'},
      {id:21,name: '伤残等级'},{id:30,name:'"定损均值'},{id:31,name:'其他费用'},{id:32,name:'医疗费定损'}];
  }

  getInjuryBasePriceList() {
  let injurySiteListUrl: string = this.urlUtils.getBackendApiUrl("injuryBasePrice/getInjuryBasePriceList");
  injurySiteListUrl = this.urlUtils.addQueryString(injurySiteListUrl, 'feetype', this.feetype);
  injurySiteListUrl = this.urlUtils.addQueryString(injurySiteListUrl, 'branchInsuranceCompanyId',this.branchInsuranceCompanyId);
  injurySiteListUrl = this.urlUtils.addQueryString(injurySiteListUrl, 'diagnosisName', this.diagnosisName);
  injurySiteListUrl = this.urlUtils.addQueryString(injurySiteListUrl, 'page', this.pageModel.currentPageNum);
  injurySiteListUrl = this.urlUtils.addQueryString(injurySiteListUrl, 'pageCount', 18);
  this.injuryBasePriceService.getInjuryBasePriceList(injurySiteListUrl).then(response => {
    this.injuryBasePriceList = response.resultList;
    this.pageModel = response;
    // console.log(response)
    // this.pageNumber = response.pageNumber;
    // this.pageCounts = response.pageCount;
  });
}

   searchInjuryBasePrice(){
     this.pageModel = {"currentPageNum":1};
     this.getInjuryBasePriceList();
   }


  getCompanyId(companyId:any){
    this.branchInsuranceCompanyId=companyId;
    this.feeBasePriceModel.branchInsuranceCompanyId =companyId;
  }

  importDate(){
    $("#importModel").modal('show');
  }

  alterCostInfo(injuryBasePrice){
    //修改
    if(injuryBasePrice == null ){
     this.OperationWinTitle="新增费用信息";
      this.saveFlag=true;
      this.feeBasePriceModel.branchInsuranceCompanyName = null;
      this.feeBasePriceModel.branchInsuranceCompanyId = null;
      this.feeBasePriceModel.basePrice = null;
      this.feeBasePriceModel.diagnosisName = null;
      this.feeBasePriceModel.diagnosisSubName = null;
      this.feeBasePriceModel.id = null;
      this.feeBasePriceModel.feetype = null;
      // 新增
    } else{
      this.OperationWinTitle="费用信息修改";
      this.saveFlag=false;
      this.feeBasePriceModel.branchInsuranceCompanyName = injuryBasePrice.branchInsuranceCompanyName;
      this.feeBasePriceModel.branchInsuranceCompanyId = injuryBasePrice.branchInsuranceCompanyId;
      this.feeBasePriceModel.basePrice = injuryBasePrice.basePrice;
      this.feeBasePriceModel.diagnosisName = injuryBasePrice.diagnosisName;
      this.feeBasePriceModel.diagnosisSubName = injuryBasePrice.diagnosisSubName;
      this.feeBasePriceModel.id = injuryBasePrice.id;
      this.feeBasePriceModel.feetype = injuryBasePrice.feetype;
      this.feeBasePriceModel.version = injuryBasePrice.version;
    }
    // 获取诊断信息

    $("#alterCostInformationWin").modal('show');
  }

  saveInjuryWin(){
    var url = this.urlUtils.getBackendApiUrl("injuryBasePrice/saveNewBasePrice");
    this.injuryBasePriceService.saveNewBasePrice(url,this.feeBasePriceModel).then(response=>{
      $("#alterCostInformationWin").modal('hide');
      this.getInjuryBasePriceList();
      layer.msg(response);
    });
  }

  updateInjuryWin(){
    var url = this.urlUtils.getBackendApiUrl("injuryBasePrice/updateBasePrice");
    this.injuryBasePriceService.saveNewBasePrice(url,this.feeBasePriceModel).then(response=>{
      $("#alterCostInformationWin").modal('hide');
      this.getInjuryBasePriceList();
      layer.msg(response);
    });
  }

  deleteCostInfo(injuryBasePrice){
    var self = this;
    layer.confirm("确定删除此记录？", {btn: ['确定', '取消']}, function (index) {
      layer.close(index);

      self.injuryBasePriceService.deleteBasePrice(injuryBasePrice).then(response=>{
        layer.msg(response);
        self.getInjuryBasePriceList();

      })
    }, function (index) {
      layer.close(index);
    });

  }

  saveImportData(){
    alert($("#upfile").val())
    var fileDir = $("#upfile").val();
    var suffix = fileDir.substr(fileDir.lastIndexOf("."));
    if("" == fileDir){
      alert("选择需要导入的Excel文件！");
      return false;
    }
    if(".xls" != suffix && ".xlsx" != suffix ){
      alert("选择Excel格式的文件导入！");
      return false;
    }
    return true;
  }


  getDiagnosNames(event,elementName){
//从后台获取诊断名称：
    var _this = this;
    var target = $(event.target);
    var diagnosNames = this.injuryBasePriceService.getDiagnosNames();

    // $("#alterdiagnoseInfo").bigAutocomplete({
    target.bigAutocomplete({
      data: diagnosNames,
      callback:function(data){
        if(elementName =="search"){
          _this.diagnosisName= data.title;

        }
        if(elementName =="alter"){
          _this.feeBasePriceModel.diagnosisMainId =data.id;
        }

         // alert(data.title);
      }
    });
    target.focus();
    // $("#alterdiagnoseInfo").focus();
  }

}
